using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using Q.Framework.ValueObject;
using Q.Framework.Extension;

namespace Q.Framework.WebControls
{
    /// <summary>
    /// Controle para preenchimento de telefones
    /// </summary>
    [DefaultProperty("Value")]
    [ToolboxData("<{0}:TelefoneControl runat=server></{0}:TelefoneControl>")]
    public class TelefoneControl : CompositeControl, IBindableControl
    {
        private QTextBox txtTelefone = new QTextBox();
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public Telefone Value
        {
            get
            {
                EnsureChildControls();

                Telefone tel = new Telefone();

                if (txtTelefone.Text.IsNullOrWhiteSpace())
                    return tel;
                if (txtTelefone.Text.RemoveMask().Replace(" ", "").Length != 10)
                    return tel;

                tel.Ddd = txtTelefone.Text.RemoveMask().Trim().Substring(0, 2).ToInt32();
                tel.Numero = txtTelefone.Text.RemoveMask().Trim().Substring(2);

                return tel;
            }
            set
            {
                EnsureChildControls();
                if (value != null && value.Numero != null)
                {
                    txtTelefone.Text = String.Format("({0}) {1}-{2}", value.Ddd.ToString().PadLeft(2, '0'), value.Numero.Substring(0, 4), value.Numero.Substring(4));
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the Web server control is enabled.
        /// </summary>
        /// <returns>true if control is enabled; otherwise, false. The default is true.</returns>
        public override bool Enabled
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                EnsureChildControls();
                txtTelefone.Enabled = value;
                base.Enabled = value;
            }
        }

        /// <summary>
        /// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object and stores tracing information about the control if tracing is enabled.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.</param>
        public override void RenderControl(HtmlTextWriter writer)
        {
            txtTelefone.RenderControl(writer);
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            txtTelefone.ID = this.ID + "Telefone";
            txtTelefone.Width = new Unit(150, UnitType.Pixel);
            txtTelefone.ValueType = TypeExpression.Telefone;
            this.Controls.Add(txtTelefone);
            base.CreateChildControls();
        }

        private Boolean required;

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="TelefoneControl"/> is required.
        /// </summary>
        /// <value>
        ///   <c>true</c> if required; otherwise, <c>false</c>.
        /// </value>
        public Boolean Required
        {
            get { return required; }
            set { required = value; txtTelefone.Required = value; }
        }


        public string DataPropertyName { get; set; }

        private String controlBindableProperty = "Value";
        public string ControlBindableProperty
        {
            get { return controlBindableProperty; }
            set { controlBindableProperty = value; }
        }
    }
}